package it.unibz.easytravel.zip;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Vector;
import java.util.zip.*;
import java.io.*;
import java.util.*;
import java.util.zip.*;

import android.app.Activity;

public class ZipManager
{
Activity a;
//   static public void main(String[]p)
//   {
//       new ZipManager().Create_Zip_File(new File("t.zip"), 
//           new File[]{new File("t")}, false);
//   }

   private void listContents( File Zip_File, File dir, ZipOutputStream out ) 
       throws Exception
   {
       // Assume that dir is a directory.  List
       // its contents, including the contents of
       // subdirectories at all levels.
       //System.out.println("Directory \"" + dir.getName() + "\"");  
       String[] files;  // The names of the files in the directory.
       files = dir.list();
       for (int i = 0; i < files.length; i++) {
       File f;  // One of the files in the directory.
       f = new File(dir, files[i]);

       if ( f.isDirectory() ) 
       {
           // Call listContents() recursively to
           // list the contents of the directory, f.
           listContents(Zip_File, f, out);
       }
       else 
       {
           // For a regular file, just print the name, files[i].
           zipEntry(Zip_File, f, out);
       }
       }
   } // end listContents()

   public void zip(String Zip_File,File[] To_Be_Zipped_Files,
       boolean Skip_Dirs, Activity a)
   {
       
	   this.a = a;
       try
       {
    
           // Open archive file
           //FileOutputStream stream=new FileOutputStream(Zip_File); 
             
	            FileOutputStream stream = a.openFileOutput(Zip_File, 1); 
	            //OutputStreamWriter osw = new OutputStreamWriter(fOut);   
	            //osw.flush(); 
	            //osw.close();
		
    	   
           ZipOutputStream out = new ZipOutputStream(stream);

           for (int i=0;i<To_Be_Zipped_Files.length;i++)
           {
               //if (To_Be_Zipped_Files[i]==null 
                 //  || !To_Be_Zipped_Files[i].exists() 
                  // || (Skip_Dirs )) 
                  // continue;

               //System.out.println("Adding "+To_Be_Zipped_Files[i].getName());

               zipEntry(new File(Zip_File), To_Be_Zipped_Files[i], out);

           }
           out.close();
           stream.close();
           //System.out.println("Finished zipping : " + Zip_File.getAbsolutePath());
       }
       catch (Exception e)
       {
           e.printStackTrace();
           //System.out.println("Error: " + e.getMessage());
           return;
       }
   } 
   private void zipEntry(File Zip_File, File file, ZipOutputStream out) 
       throws Exception
   {
       if (file.isDirectory())
       {
           listContents(Zip_File, file, out);
           return;
       }

       int BUFFER_SIZE=1024000;                                     // 1 M
       byte buffer[]= new byte[BUFFER_SIZE];
       char buff[] = new char[BUFFER_SIZE];
       // Add archive entry
       
       String entry = file.getName();
       ZipEntry zipAdd = new ZipEntry(entry);
       
       //zipAdd.setTime(file.lastModified());

       out.putNextEntry(zipAdd);
       // Read input & write to output
       FileInputStream fIn = a.openFileInput(file.getName()); 
       InputStreamReader isr = new InputStreamReader(fIn);
       int num = isr.read(buff);
 
       buffer = new String(buff).getBytes();
       //FileInputStream in = new FileInputStream(file.getAbsolutePath());
       //int len;
       //int nRead=in.available();
       out.write(buffer, 0, num);
       
//       while(nRead > 0)
//       {
//           out.write(buffer, 0, nRead);
//       }
//       
//       in.close();

       out.closeEntry();
       
   }
}